package nowcoder.vivo3;

import com.sun.jersey.spi.resource.Singleton;

import java.util.HashSet;
import java.util.Scanner;

public class Main {

    private static int[] nodes;
    private static HashSet<Integer>[] rels;


    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        String s2 = sc.nextLine();
        String[] ss1 = s1.split(",");
        String[] ss2 = s2.split(";");
        nodes = new int[ss1.length];
        rels = new HashSet[nodes.length];
        for(int i = 0; i < nodes.length; i++){
            rels[i] = new HashSet<>();
            nodes[i] = Integer.parseInt(ss1[i]);
            for(String s : ss2[i].split(",")){
                int t = Integer.parseInt(s);
                if(t!=0) rels[i].add(t-1);
            }

        }
        int max = 0;
        for(int i = 0; i < nodes.length; i++){
            max = Math.max(max, dfs(i, 0));
        }
        System.out.println(max);
    }

    private static int dfs(int curr, int sum){
        if(rels[curr].isEmpty()){
            return sum+nodes[curr];
        }else{
            int min = Integer.MAX_VALUE;
            for(int i : rels[curr]){
                min = Math.min(dfs(i, sum+nodes[curr]), min);
            }
            return min;
        }
    }

}
